package com.education.welco.controller;

import com.education.auth.aspectannotation.BusinessDiary;
import com.education.common.core.BaseController;
import com.education.common.utils.ApiReturnObject;
import com.education.common.utils.StringUtils;
import com.education.welco.feign.userFeign;
import com.education.welco.pojo.*;
import com.education.welco.service.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 教师私有备课目录 信息操作处理
 *
 * @author d
 * @date 2020-05-08
 */
@RestController
@RequestMapping("/sCatalogMenu")
public class SCatalogMenuController extends BaseController {

    @Autowired
    private ISCatalogMenuService catalogMenuService;
    @Autowired
    private ISCatalogClassTeaService catalogClassTeaService;//在授班级
    @Autowired
    private ISCatalogClassService catalogClassService;//分享班级
    @Autowired
    private ITClassService classService;
    @Autowired
    private ITSchooldeptService schooldeptService;
    @Autowired
    private ISCatalogClassEndService catalogClassEndService;
    @Autowired
    private ISResourcesService resourcesService;
    @Autowired
    private ISCatalogService catalogService;
    @Autowired
    private userFeign userFeign;

    /**
     * 查询全部学校课程
     *
     * @return
     */
    @ApiOperation(value = "查询全部学校课程--二期同步使用", httpMethod = "GET")
    @GetMapping("/SelectCatalogList")
    public ApiReturnObject SelectCatalogList() {
        List<SCatalogMenu> catalogMenuList = catalogMenuService.selectSCatalogMenuByParentId(0);
        return new ApiReturnObject("200", "success", catalogMenuList);
    }

    /**
     * 单独查询在授班级
     *
     * @param catalogId
     * @return
     */
    @SuppressWarnings("all")
    @GetMapping("/onlyCrown")
    public ApiReturnObject oulyCrown(Integer catalogId) {
        if (catalogId != null) {
            //id name   父级部门名称
            List<TClass> tClasses = classService.selectTClassByClassId(catalogId);
            //遍历添加部门信息
            return getApiReturnObject(tClasses);
        }
        return new ApiReturnObject("201", "缺少参数catalogId", "");
    }

    /**
     * 单独查询在授班级    针对于分享班级的查询
     *
     * @param catalogId
     * @return
     */
    @GetMapping("/oulyCrownLook")
    public ApiReturnObject oulyCrownLook(Integer catalogId) {
        if (catalogId != null) {
            //id name   父级部门名称
            List<TClass> tClasses = classService.selectTClassByClassIdAndLook(catalogId);
            //遍历添加部门信息
            return getApiReturnObject(tClasses);
        }
        return new ApiReturnObject("201", "缺少参数catalogId", "");
    }

    /**
     * 班级补充完善信息
     *
     * @param tClasses
     * @return
     */
    public ApiReturnObject getApiReturnObject(List<TClass> tClasses) {
        if (tClasses.size() > 0) {
            for (TClass tClass : tClasses) {
                TClass tClass1 = classService.selectTClassById(tClass.getId());
                tClass.setDeptId(tClass1.getId());
                tClass.setDeptName(tClass1.getClassName());
            }
        }
        return new ApiReturnObject("200", "查询成功", tClasses);
    }

    /**
     * 单独查询完结班级
     *
     * @param catalogId
     * @return
     */
    @GetMapping("/onlyOver")
    public ApiReturnObject onlyOver(Integer catalogId) {
        if (catalogId != null) {
            //id name   父级部门名称
            List<TClass> tClasses = classService.selectOverTClassByClassId(catalogId);
            //遍历添加部门信息
            return getApiReturnObject(tClasses);
        }
        return new ApiReturnObject("201", "缺少参数catalogId", "");
    }

    /**
     * 单独查询分享班级
     *
     * @param catalogId
     * @return
     */
    @GetMapping("/onlyLook")
    public ApiReturnObject onlyLook(Integer catalogId) {
        if (catalogId != null) {
            //id name   父级部门名称  分享班级列表
            List<TClass> tClasses = classService.selectLookTClassByClassId(catalogId);
            //遍历添加部门信息
            return getApiReturnObject(tClasses);
        }
        return new ApiReturnObject("201", "缺少参数catalogId", "");
    }

    /**
     * 添加在授班级
     *
     * @param list
     * @return
     */
    public List<SCatalogMenu> addClass(List<SCatalogMenu> list) {
        for (SCatalogMenu sCatalogMenu : list) {
            //id name   父级部门名称
            List<TClass> tClasses = classService.selectTClassByClassId(sCatalogMenu.getId());
            if (tClasses.size() > 3) {
                tClasses = tClasses.subList(0, 3);
            }
            sCatalogMenu.setClassList(tClasses);
        }
        return list;
    }

    /**
     * 查询当前老师备课中心的备课列表
     *
     * @param catalogMenu userId: 1207  老师用户id
     *                    catalogType:  1.理论课堂;2.实训课堂
     * @return
     */
    @GetMapping("/list")
    @BusinessDiary(methodUrl = "/sCatalogMenu/list", record = "查询当前老师的备课列表", opeTypeName = "查询", opeTypeValue = "1")
    public ApiReturnObject list(SCatalogMenu catalogMenu) {
        //根据课程id 查询出课程下目录
        if (catalogMenu.getUserId() != null) {
            //根据用户id~查出学校id
            Map<String, Object> map = userFeign.findByUserId(catalogMenu.getUserId());
            Integer schoolId = (Integer) map.get("schoolId");
            if (schoolId == 109) {
                catalogMenu.setDeptId(schoolId);
                catalogMenu.setUserId(null);
            } else {
                //查询出备课列表
                catalogMenu.setUserId(catalogMenu.getUserId());
            }
            catalogMenu.setParentId(0);
            List<SCatalogMenu> list = catalogMenuService.selectSCatalogMenuListTwo(catalogMenu);
            //添加在授班级
            //List<SCatalogMenu> catalogMenuList = addClass(list);
            return new ApiReturnObject("200", "查询成功", list);
        } else {
            return new ApiReturnObject("201", "缺少userId参数", "");
        }
    }

    /**
     * 判断教学大纲
     *
     * @param id
     * @return
     */
    @GetMapping("selectOneById")
    public ApiReturnObject selectOneById(Integer id) {
        if (id != null) {
            SCatalogMenu catalogMenu = catalogMenuService.selectSCatalogMenuById(id);
            catalogMenu.setOutline("0");
            //判断是否有教学大纲  将id做为parentId进行查询下级
            List<SCatalogMenu> catalogMenuList = catalogMenuService.selectSCatalogMenuByParentId(catalogMenu.getId());
            if (catalogMenuList.size() > 0) {
                //设置标志为可以点击  其存在目录
                catalogMenu.setOutline("1");
                //遍历目录判断目录是否符合规范
                for (SCatalogMenu menu : catalogMenuList) {
                    //判断目录是否拥有子级
                    List<SCatalogMenu> catalogMenuList2 = catalogMenuService.selectSCatalogMenuByParentId(menu.getId());
                    if (!(catalogMenuList2.size() > 0)) {
                        catalogMenu.setOutline("2");
                    }
                }
            }
            return new ApiReturnObject("200", "查询成功", catalogMenu);
        }
        return new ApiReturnObject("201", "缺少参数", "");
    }

    /**
     * 查询当前老师已同步到上课资料的备课列表
     *
     * @param catalogMenu
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/synList")
    public ApiReturnObject listOther2(SCatalogMenu catalogMenu, Integer pageNum, Integer pageSize) {
        //根据课程id 查询出 之下的目录
        if (catalogMenu.getUserId() != null) {
            //查询出备课列表以及在授班级
            catalogMenu.setParentId(0);
            catalogMenu.setIsSyn("1");
            if (pageNum != null && pageSize != null) {
                PageHelper.startPage(pageNum, pageSize);
            }
            List<SCatalogMenu> list = catalogMenuService.selectSCatalogMenuList(catalogMenu);
            PageInfo<SCatalogMenu> pageInfo = new PageInfo<>(list);
            return new ApiReturnObject("200", "查询成功", pageInfo);
        } else {
            return new ApiReturnObject("201", "缺少userId参数", "");
        }
    }

    /**
     * 查询一级课程 预约录制时使用
     *
     * @param userId
     * @return
     */
    @GetMapping("/findFirstCatalog")
    public ApiReturnObject findFirstCatalog(Integer userId) {
        if (userId != null) {
            SCatalogMenu catalogMenu = new SCatalogMenu();
            //catalogMenu.setIsSyn("1");
            catalogMenu.setParentId(0);
            catalogMenu.setUserId(userId);
            catalogMenu.setCatalogType("1");
            List<SCatalogMenu> catalogMenuList = catalogMenuService.selectSCatalogMenuList(catalogMenu);
            return new ApiReturnObject("200", "查询成功", catalogMenuList);
        }
        return new ApiReturnObject("201", "缺少userId", "");
    }

    /**
     * 查询目录列表
     *
     * @param cList
     * @return
     */
    public List<SCatalogMenu> listSon(List<SCatalogMenu> cList) {
        for (SCatalogMenu catalogMenu : cList) {
            //根据第一级的id查询出二级列表
            List<SCatalogMenu> jrCatalogs = catalogMenuService.selectSCatalogMenuByParentId(catalogMenu.getId());
            if (jrCatalogs != null && jrCatalogs.size() > 0) {
                //设置子级集合
                catalogMenu.setChildren(listSon(jrCatalogs));
            }
        }
        return cList;
    }

    /**
     * 查询目录列表
     *
     * @param cList
     * @return
     */
    public SCatalogMenu listSon2(SCatalogMenu cList) {
        //根据课程id查询出一级目录
        List<SCatalogMenu> jrCatalogs = catalogMenuService.selectSCatalogMenuByParentId(cList.getId());
        if (jrCatalogs != null && jrCatalogs.size() > 0) {
            //设置子级集合
            cList.setChildren(listSon(jrCatalogs));
        }
        return cList;
    }

    /**
     * 进行两级列表查询 我的资源库 选择课程目录时使用
     *
     * @return
     */
    @GetMapping("/directoryList")
    public ApiReturnObject directoryList(/*Integer catalogId*/) {
        //根据课程id 查询出 之下的目录
        SCatalogMenu sCatalogMenu = new SCatalogMenu();
        //查询出第一级列表
        sCatalogMenu.setParentId(0);
        //第一级列表
        List<SCatalogMenu> list = catalogMenuService.selectSCatalogMenuList6(sCatalogMenu);
        //第二级列表
        for (SCatalogMenu catalogMenu : list) {
            List<SCatalogMenu> catalogMenuList = catalogMenuService.selectSCatalogMenuByParentId2(catalogMenu.getId());
            if (catalogMenuList != null && catalogMenuList.size() > 0) {
                catalogMenu.setChildren(catalogMenuList);
            }
        }
        //List<SCatalogMenu> sCatalogMenus = listSon(list);
        return new ApiReturnObject("200", "查询成功", list);
    }

    /**
     * 根据id查询下级
     *
     * @param id
     * @return
     */
    @GetMapping("/dirDown")
    public ApiReturnObject dirDown(Integer id) {
        if (id != null) {
            SCatalogMenu catalogMenu = catalogMenuService.selectSCatalogMenuById(id);
            //子级列表
            List<SCatalogMenu> catalogMenuList = catalogMenuService.selectSCatalogMenuByParentId3(id);
            for (SCatalogMenu sCatalogMenu : catalogMenuList) {
                sCatalogMenu.setDeptId(catalogMenu.getId());
                sCatalogMenu.setDeptName(catalogMenu.getName());
            }
            return new ApiReturnObject("200", "查询成功", catalogMenuList);
        }
        return new ApiReturnObject("201", "缺少参数", "");
    }

    /**
     * 展示课程以及下面的子列表 编辑教学大纲
     *
     * @param catalogId
     * @return
     */
    @GetMapping("/listOther")
    public ApiReturnObject listOther(Integer catalogId) {
        //根据课程id 查询出 之下的目录
        if (catalogId != null) {
            SCatalogMenu sCatalogMenu = new SCatalogMenu();
            //查询出第一级列表
            sCatalogMenu.setParentId(0);
            sCatalogMenu.setId(catalogId);
            List<SCatalogMenu> list = catalogMenuService.selectSCatalogMenuList(sCatalogMenu);
            //一级 以及 下面子列表
            List<SCatalogMenu> sCatalogMenus = listSon(list);
            return new ApiReturnObject("200", "查询成功", sCatalogMenus);
        } else {
            return new ApiReturnObject("201", "缺少相关参数", "");
        }
    }

    /**
     * 教学大纲展示课程以及下面的子列表
     *
     * @param catalogId
     * @return
     */
    @GetMapping("/listOthers")
    public ApiReturnObject listOthers(Integer catalogId) {
        //根据课程id 查询出之下的目录
        if (catalogId != null) {
            SCatalogMenu sCatalogMenu = new SCatalogMenu();
            //查询出第一级列表
            sCatalogMenu.setParentId(0);
            sCatalogMenu.setId(catalogId);
            SCatalogMenu list = catalogMenuService.selectSCatalogMenuList2(sCatalogMenu);
            //一级以及下面子列表
            SCatalogMenu sCatalogMenus = new SCatalogMenu();
            if (list.getId() != null) {
                sCatalogMenus = listSon2(list);
            }
            if (sCatalogMenus.getChildren() == null) {
                return new ApiReturnObject("202", "暂无教学大纲，请先编辑教学大纲", "");
            }
            return new ApiReturnObject("200", "查询成功", sCatalogMenus);
        } else {
            return new ApiReturnObject("201", "缺少相关参数", "");
        }
    }

    /**
     * 添加备课时 显示系统图片列表
     *
     * @return
     */
    @GetMapping("/SystemImage")
    public ApiReturnObject SystemImage() {
        ArrayList<String> strings = new ArrayList<>();
        strings.add("1589594912830.png");
        strings.add("1589594934671.png");
        strings.add("1589594953601.png");
        strings.add("1589594980085.png");
        strings.add("1589595026613.png");
        return new ApiReturnObject("200", "查询成功", strings);
    }

    /**
     * 备课中心-新增课程
     *
     * @param catalogMenu
     * @return
     */
    @PostMapping("/add")
    public ApiReturnObject addSave(@RequestBody SCatalogMenu catalogMenu) {
        if (catalogMenu.getUserId() != null) {
            catalogMenu.setCreateTime(new Date());
            catalogMenu.setIsStu("1");
            catalogMenuService.insertSCatalogMenu(catalogMenu);
            //获取到添加的课程的id
            Integer cId = catalogMenu.getId();
            return new ApiReturnObject("200", "添加成功", cId);
        }
        return new ApiReturnObject("201", "缺少课程id", "");
    }

    /**
     * 备课中心-课程详情
     *
     * @param id 课程id
     * @return
     */
    @GetMapping("/catalogInfo")
    public ApiReturnObject catalogInfo(@RequestParam Integer id) {
        SCatalogMenu sCatalogMenu = catalogMenuService.selectSCatalogMenuById(id);
        return new ApiReturnObject("200", "查询成功", sCatalogMenu);
    }

    /**
     * 备课中心-课程更新
     *
     * @param catalogMenu 课程
     * @return
     */
    @PostMapping("/updateCatalogById")
    public ApiReturnObject updateCatalogById(@RequestBody SCatalogMenu catalogMenu) {
        int i = catalogMenuService.updateSCatalogMenu(catalogMenu);
        if (i == 0) {
            return new ApiReturnObject("201", "编辑失败", "");
        }
        return new ApiReturnObject("200", "编辑成功", "");
    }

    /**
     * 判断现在要添加的大纲是第几级
     *
     * @param parentId
     * @param i
     * @return
     */
    public Integer testClass(Integer parentId, int i) {
        SCatalogMenu catalogMenu = catalogMenuService.selectSCatalogMenuById(parentId);
        if (catalogMenu != null) {
            i = i + 1;
            i = testClass(catalogMenu.getParentId(), i);
        }
        return i;
    }

    /**
     * 设置封面 跟随课程
     *
     * @param catalogId
     * @return
     */
    private String findIcoByCatalogId(Integer catalogId) {
        SCatalogMenu sCatalogMenu = catalogMenuService.selectSCatalogMenuById(catalogId);
        if (sCatalogMenu.getParentId() != 0) {
            return findIcoByCatalogId(sCatalogMenu.getParentId());
        } else {
            return sCatalogMenu.getCatalogIco();
        }
    }

    /**
     * 添加备课基础信息 教学大纲
     *
     * @param catalogMenu
     * @return
     */
    @PostMapping("/addOutline")
    public ApiReturnObject addOutline(@RequestBody SCatalogMenu catalogMenu) {
        if (catalogMenu.getParentId() != null) {
            //判断第几级 要不要往另一张表中添加
            if (catalogMenu.getParentId() != 0) {
                int i = 0;
                Integer integer = testClass(catalogMenu.getParentId(), i);
                if (integer > 2) {
                    return new ApiReturnObject("202", "只能添加两级目录", "");
                }
                catalogMenu.setCreateTime(new Date());
                catalogMenuService.insertSCatalogMenu(catalogMenu);
                if (integer == 2) {
                    //往另一张表中添加
                    SCatalog catalog = new SCatalog();
                    catalog.setCreateTime(new Date());
                    catalog.setCatalogTeacherid(catalogMenu.getUserId().toString());
                    catalog.setCatalogName(catalogMenu.getName());
                    catalog.setParentId(catalogMenu.getId());
                    catalog.setCatalogState(1);
                    //设置封面 跟随课程
                    String catalogIco = findIcoByCatalogId(catalogMenu.getId());
                    catalog.setCatalogIco(catalogIco);
                    catalogService.insertSCatalog(catalog);
                }
                return new ApiReturnObject("200", "添加成功", "");
            }
            catalogMenu.setCreateTime(new Date());
            catalogMenuService.insertSCatalogMenu(catalogMenu);
            return new ApiReturnObject("200", "添加成功", "");
        }
        return new ApiReturnObject("201", "缺少父级id", "");
    }

    /**
     * 同步上课资料
     *
     * @param id
     * @return
     */
    @GetMapping("/updateIssyn")
    public ApiReturnObject updateIssyn(Integer id) {
        if (id != null) {
            SCatalogMenu catalogMenu1 = catalogMenuService.selectSCatalogMenuById(id);
            if ("1".equals(catalogMenu1.getIsSyn())) {
                //已同步
                return new ApiReturnObject("202", "此课程已同步", "");
            }
            SCatalogMenu catalogMenu = new SCatalogMenu();
            catalogMenu.setId(id);
            catalogMenu.setIsSyn("1");
            catalogMenuService.updateSCatalogMenu(catalogMenu);
            return new ApiReturnObject("200", "同步成功", "");
        }
        return new ApiReturnObject("201", "缺少参数", "");
    }

    /**
     * 修改在授班级  删除中间表数据 重新添加
     */
    @PostMapping("/updateCrownClass")
    public ApiReturnObject updateCrownClass(@RequestBody SCatalogMenu catalogMenu) {
        //根据课程id    再重新添加
        if (catalogMenu.getId() != null) {
            //删除 在授班级中间表数据
            catalogClassTeaService.deleteSCatalogClassTeaByCatalogId(catalogMenu.getId());
            if (catalogMenu.getClassList().size() > 0) {
                SCatalogClassTea sCatalogClassTea = new SCatalogClassTea();
                sCatalogClassTea.setCreateTime(new Date());
                sCatalogClassTea.setCatalogId(catalogMenu.getId());
                sCatalogClassTea.setUserId(catalogMenu.getUserId());
                //进行添加
                for (TClass tClass : catalogMenu.getClassList()) {
                    //classId 集合
                    sCatalogClassTea.setClassId(tClass.getId());
                    catalogClassTeaService.insertSCatalogClassTea(sCatalogClassTea);
                }
            }
            return new ApiReturnObject("200", "添加成功", "");
        }
        return new ApiReturnObject("201", "缺少catalogId", "");
    }

    /**
     * 修改完结班级  删除中间表数据 重新添加
     */
    @PostMapping("/updateEndClass")
    public ApiReturnObject updateEndClass(@RequestBody SCatalogMenu catalogMenu) {
        //根据课程id    再重新添加
        if (catalogMenu.getId() != null) {
            //删除 在授班级中间表数据
            catalogClassEndService.deleteSCatalogClassEndByCatalogId(catalogMenu.getId());
            if (catalogMenu.getClassEndList().size() > 0) {
                SCatalogClassEnd catalogClassEnd = new SCatalogClassEnd();
                catalogClassEnd.setCreateTime(new Date());
                catalogClassEnd.setCatalogId(catalogMenu.getId());
                catalogClassEnd.setUserId(catalogMenu.getUserId());
                //进行添加  并删除在授班级中的数据
                for (TClass classEnd : catalogMenu.getClassEndList()) {
                    //删除在授班级数据  根据 课程id 和 班级id
                    catalogClassTeaService.deleteSCatalogClassTeaByCatalogIdAndClassId(catalogMenu.getId(), classEnd.getId());
                    //classId 集合
                    catalogClassEnd.setClassId(classEnd.getId());
                    catalogClassEndService.insertSCatalogClassEnd(catalogClassEnd);
                }
            }
            return new ApiReturnObject("200", "添加成功", "");
        }
        return new ApiReturnObject("201", "缺少catalogId", "");
    }

    /**
     * 修改分享班级  删除中间表数据 重新添加
     */
    @PostMapping("/updateLookClass")
    public ApiReturnObject updateLookClass(@RequestBody SCatalogMenu catalogMenu) {
        //根据课程id    再重新添加
        if (catalogMenu.getId() != null) {
            //删除 分享班级中间表数据
            catalogClassService.deleteSCatalogClassByCatalogId(catalogMenu.getId());
            if (catalogMenu.getClassLooKList() != null && catalogMenu.getClassLooKList().size() > 0) {
                SCatalogClass catalogClass = new SCatalogClass();
                catalogClass.setCreateTime(new Date());
                catalogClass.setCatalogId(catalogMenu.getId());
                catalogClass.setUserId(catalogMenu.getUserId());
                //进行添加
                for (TClass classLook : catalogMenu.getClassLooKList()) {
                    //classId 集合
                    catalogClass.setClassId(classLook.getId());
                    catalogClassService.insertSCatalogClass(catalogClass);
                }
            }
            return new ApiReturnObject("200", "添加成功", "");
        }
        return new ApiReturnObject("201", "缺少catalogId", "");
    }

    /**
     * 修改教学大纲
     */
    @PostMapping("/update")
    public ApiReturnObject editSave(@RequestBody SCatalogMenu catalogMenu) {
        if (catalogMenu.getId() != null) {
            catalogMenuService.updateSCatalogMenu(catalogMenu);
            if (StringUtils.isNotNull(catalogMenu.getId()) && !"".equals(catalogMenu.getName())) {
                //根据id 查询是否在学校课程表中有关联数据，进行改变
                SCatalog sCatalog = new SCatalog();
                sCatalog.setParentId(catalogMenu.getId());
                List<SCatalog> sCatalogs = catalogService.selectSCatalogList(sCatalog);
                if (sCatalogs.size() > 0) {
                    SCatalog catalog1 = new SCatalog();
                    for (SCatalog catalog : sCatalogs) {
                        catalog1.setId(catalog.getId());
                        catalog1.setCatalogName(catalogMenu.getName());
                        catalogService.updateSCatalog(catalog1);
                    }
                }
            }
            return new ApiReturnObject("200", "修改成功", "");
        }
        return new ApiReturnObject("201", "缺少参数", "");
    }

    /**
     * 修改备课课程
     */
    @PostMapping("/updateCatalog")
    public ApiReturnObject editSave2(@RequestBody SCatalogMenu catalogMenu) {
        if (catalogMenu.getId() != null) {
            //判断有没有修改在授班级
            if (catalogMenu.getClassList() != null && catalogMenu.getClassList().size() > 0) {
                for (TClass tClass : catalogMenu.getClassList()) {
                    SCatalogClassTea sCatalogClassTea = new SCatalogClassTea();
                    sCatalogClassTea.setCreateTime(new Date());
                    sCatalogClassTea.setCatalogId(catalogMenu.getId());
                    catalogClassTeaService.insertSCatalogClassTea(sCatalogClassTea);
                }
            }
            //分享班级
            if (catalogMenu.getClassLooKList() != null && catalogMenu.getClassLooKList().size() > 0) {
                SCatalogClass sCatalogClass = new SCatalogClass();
                sCatalogClass.setCreateTime(new Date());
                sCatalogClass.setCatalogId(catalogMenu.getId());
                for (TClass tClass : catalogMenu.getClassLooKList()) {
                    sCatalogClass.setClassId(tClass.getId());
                    catalogClassService.insertSCatalogClass(sCatalogClass);
                }
            }
            catalogMenuService.updateSCatalogMenu(catalogMenu);
            return new ApiReturnObject("200", "修改成功", "");
        }
        return new ApiReturnObject("201", "缺少参数", "");
    }

    //根据课程对象进行递归
    //查询目录列表
    public List<Integer> listSonDel1(List<Integer> cList, Integer id) {
        List<SCatalogMenu> jrCatalogs = catalogMenuService.selectSCatalogMenuByParentId(id);
        if (jrCatalogs != null && jrCatalogs.size() > 0) {
            for (SCatalogMenu jrCatalog : jrCatalogs) {
                //继续执行
                for (Integer integer : listSonDel1(cList, jrCatalog.getId())) {
                    cList.add(integer);
                }
            }
        }
        return cList;
    }

    /**
     * 删除课程
     */
    @PostMapping("/del")
    public ApiReturnObject remove(Integer id) {
        if (id != null) {
            //代表删除的课程不是最后一级
            List<Integer> list = new ArrayList<>();
            list.add(id);
            List<Integer> list1 = listSonDel1(list, id);
            //拿到id集合 进行删除
            for (Integer integer : list1) {
                //删除的是第一级或者第二级的话 删除目录以及下面的各种资源
                catalogMenuService.deleteSCatalogMenuById(id);
            }
            return new ApiReturnObject("200", "删除成功", "");
        }
        return new ApiReturnObject("201", "缺少相关参数", "");
    }

    /**
     * 关闭/开启-课程
     */
    @GetMapping("/delOnlyCatalog")
    public ApiReturnObject delOnlyCatalog(Integer id, String delFlag) {
        if (id == null || StringUtils.isBlank(delFlag)) {
            return new ApiReturnObject("201", "缺少相关参数", "");
        }
        SCatalogMenu sCatalogMenu = new SCatalogMenu();
        sCatalogMenu.setId(id);
        sCatalogMenu.setDelFlag(delFlag);
        catalogMenuService.updateSCatalogMenu(sCatalogMenu);
        if ("1".equals(delFlag)) {
            return new ApiReturnObject("200", "关闭课程成功", delFlag);
        }
        return new ApiReturnObject("200", "开启课程成功", delFlag);
    }

    /**
     * 删除课程
     */
    @GetMapping("/delOnlyLog")
    public ApiReturnObject delOnlyLog(Integer id) {
        if (id != null) {
            //如果下面存在子级目录 或者 存在资源 删除失败
            List<SCatalogMenu> catalogMenuList = catalogMenuService.selectSCatalogMenuByParentId(id);
            if (catalogMenuList != null && catalogMenuList.size() > 0) {
                return new ApiReturnObject("201", "该目录存在子目录,请先删除子目录", "");
            }
            SResources resources = new SResources();
            resources.setRCatalogid(id);
            List<SResources> sResources = resourcesService.selectSResourcesList(resources);
            if (sResources != null && sResources.size() > 0) {
                for (SResources sResource : sResources) {
                    if (sResource.getType() != null && !"6".equals(sResource.getType()) && !"7".equals(sResource.getType())) {
                        return new ApiReturnObject("201", "该目录下面存在资源，请先删除资源", "");
                    }
                }
            }
            //删除的是第一级或者第二级的话 删除目录以及下面的各种资源
            catalogMenuService.deleteSCatalogMenuById(id);
            return new ApiReturnObject("200", "删除成功", "");
        }
        return new ApiReturnObject("201", "缺少相关参数", "");
    }

}